(function ($) {
    /**
     * 缓存名称
     * @type {string}
     * @private
     */
    var _cacheName = 'uiButton';
    /**
     * @name uiButton
     * @class 按钮
     * @constructor
     * @summary 构造函数
     * @param {Object|String} options 如果为Object，则表示组件配置，如果为String，则调用组件方法
     * @param {Object} param 当options为String时，可以传入方法参数
     * @returns {jQuery} jQuery对象
     * @example
     *  //采用默认值
     * $('#btn').uiButton();
     * //设置组件参数
     *  $('#btn').uiButton({id:'btn',label:'提交'});
     * //调用组件方法
     * $('#btn').uiButton('setLabel','保存');
     */
    $.fn.uiButton = function (options, param) {
        if (typeof options == 'string') {
            return $.fn.uiButton.methods[options](this, param);
        }
        return this.each(function(){
            var me = $(this);
            var state = me.data(_cacheName);

            options = options || {};
            if (state) {
                $.extend(state.options, options);
            } else {
                me.data(_cacheName, {
                    options: $.extend({}, $.fn.uiButton.defaults, me.parseUIConfig(), options)
                });
            }
            _init(me);
        });
    };
    /**
     * 方法
     * @lends uiButton.prototype
     */
    $.fn.uiButton.methods = {
        /**
         * @summary 获取组件配置参数
         * @function
         * @name uiButton#options
         * @returns {Object}
         * @example
         *  $('#btn').uiButton('options');
         */
        options: function (target) {
            return $.data(target[0], _cacheName).options;
        },
        /**
         * @summary 设置按钮的样式类
         * @function
         * @name uiButton#setStyle
         * @param style {String}
         * @example
         *  $('#btn').uiButton('setStyle', 'myClassName');
         */
        setStyle: function (target, style) {
            _setStyle(target, style);
        },
        /**
         * @summary 设置按钮的显示文本
         * @function
         * @name uiButton#setLabel
         * @param text {String} 显示文本
         * @example
         *  $('#btn').uiButton('setLabel', '提交');
         */
        setLabel: function (target, text) {
            _setLabel(target, text);
        },
        /**
         * @summary 获取按钮的显示文本
         * @function
         * @name uiButton#getLabel
         * @returns {String} 显示文本
         */
        getLabel: function (target) {
            return $.data(target[0], _cacheName).options.label;
        },
        /**
         * @summary 设置按钮的图标
         * @function
         * @name uiButton#setIcons
         * @param icons {Object} 图标样式
         * @example
         *  $('#btn').uiButton('setIcons', {left:'add',right:'delete'});
         */
        setIcons: function (target, icons) {
            _setIcon(target, icons);
        },
        /**
         * @summary 启用组件
         * @function
         * @name uiButton#enable
         * @returns {jQuery} 方法调用者
         */
        enable: function (target) {
            _setDisabled(target, false);
            return target;
        },
        /**
         * @summary 禁用组件
         * @function
         * @name uiButton#disable
         * @returns {jQuery} 方法调用者
         */
        disable: function (target) {
            _setDisabled(target, true);
            return target;
        },
        /**
         * @summary 设置是否禁用组件
         * @function
         * @name uiButton#setDisabled
         * @param disabled {Boolean} 是否禁用
         * @returns {jQuery} 方法调用者
         */
        setDisabled: function (target, disabled) {
            _setDisabled(target, !!disabled);
            return target;
        }
    };
    /**
     * 默认配置
     * @lends uiButton.prototype
     */
    $.fn.uiButton.defaults =
    {
        /**
         * @summary 唯一标识
         * @type String
         * @default ""
         * @example
         * $('<button></button>').uiButton({id:'btnOK'});
         */
        id: '',
        /**
         * @summary 显示按钮图标，left表示左图标，right表示右图标，取值均为样式类。
         * @type Object
         * @default {left: '', right: ''}
         */
        icons: {left: '', right: ''},
        /**
         * 属性说明：(左侧图标：left;右侧图标：right)<br/><br/>
         * <TABLE cellSpacing="1" width="100%" class="params">
         * <TR>
         * <TH width="12%">属性</TH>
         * <TH width="88%">说明</TH></TR>
         * <TR><TD align=center>icon</TD><TD>图标样式</TD></TR>
         * <TR><TD align=center>color</TD><TD>颜色</TD></TR>
         * <TR><TD align=center>size</TD><TD>大小（像素）</TD></TR>
         * </TABLE>
         * @type Object
         * @default {left: {}, right: {}}
         * @summary 使用奥森字体图标，示例{left: {icon:'fa-comments-o',color:'#FF0000',size:20}}
         */
        fontIcon: {left: {}, right: {}},
        /**
         * @summary 显示文本（可以为HTML）
         * @type String
         * @default ''
         */
        label: '',
        /**
         * @summary 按钮样式
         * @type String
         * @default ''
         */
        style: 'button',
        /**
         * @summary 是否禁用
         * @type Boolean
         * @default false
         */
        disabled: false,
        /**
         * @summary 按钮单击时触发的方法
         * @name uiButton#onClick
         * @event
         * @default emptyFn
         */
        onClick: function () {
        }
    };
//////--------------------------------内部方法------------------------------
    /**
     * 创建按钮
     * @param target 目标对象
     */
    var _init = function (target) {
        var options = $.data(target[0], _cacheName).options;
        var content = target.contents();
        options.id = options.id || target[0].id;
        target.attr("id", options.id);
        if (_IEunder7) {
            target.attr("hidefocus", true);//去掉焦点时的虚线框
        }
        if (options.onClick) {
            target.unbind('.' + _cacheName).bind('click.' + _cacheName, options.onClick);
        }
        //当按钮内没有内容时配置生效
        if (content.length == 0) {
            if (options.label) {
                $('<span class="btnLabel">' + options.label + '</span>').appendTo(target);
            }
        }
        else {
            var lab = target.find("span.btnLabel");
            if (lab.length > 0) {
                options.label = lab.html();
            }
            else {
                options.label = target.text();
                if (options.label) {
                    target.wrapInner('<span class="btnLabel"></span>');
                }
            }
        }
        _setIcon(target, options.icons, options.fontIcon);
        _setDisabled(target, options.disabled);
        return target;
    };

    var _setLabel = function (target, text) {
        var options = $.data(target[0], _cacheName).options;
        options.label = text;
        var lab = target.find('span.btnLabel');
        lab.html(text);
    };

    var _setIcon = function (target, icons, fontIcons) {
        fontIcons = fontIcons || {};
        var options = $.data(target[0], _cacheName).options;
        if (icons || fontIcons) {
            var lab = target.find('span.btnLabel');
            var icoDisabledClass = "";
            if (options.disabled) {
                icoDisabledClass = " iconDisabled";
            }
            function setIcon(region) {
                var iconsRegion, fontIconsRegion, iconsOpts, fontIconsOpts;
                if (region == 'left') {
                    iconsRegion = icons.left;
                    fontIconsRegion = fontIcons.left;
                    iconsOpts = options.icons.left;
                    fontIconsOpts = options.fontIcon.left;
                } else if (region == 'right') {
                    iconsRegion = icons.right;
                    fontIconsRegion = fontIcons.right;
                    iconsOpts = options.icons.right;
                    fontIconsOpts = options.fontIcon.right;
                }
                if (iconsRegion || fontIconsRegion) {
                    var icon = target.find('span.icon_' + region);
                    if (icon.length > 0) {
                        icon.removeClass().addClass("icon_" + region + " " + icoDisabledClass);
                    }
                    else {
                        var icon = $('<span class="icon_' + region + ' ' + icoDisabledClass + '"></span>');
                        if (lab.length > 0) {
                            if (region == 'left') {
                                lab.before(icon);
                            } else if (region == 'right') {
                                lab.after(icon);
                            }
                        }
                        else {
                            target.append(icon);
                        }
                    }
                    if (fontIconsRegion && fontIconsRegion.icon && !_IEunder6) {
                        fontIconsOpts.size = fontIconsRegion.size || 16;
                        fontIconsOpts.color = fontIconsRegion.color || 'inherit';
                        icon.addClass('fa').addClass(fontIconsRegion.icon)
                            .css({fontSize: fontIconsOpts.size, color: fontIconsOpts.color});
                    } else {
                        if (iconsRegion) {
                            iconsOpts = iconsRegion;
                            icon.addClass('icon ' + iconsRegion);
                        }
                    }
                }
            }

            setIcon('left');
            setIcon('right');
        }
    };

    /**
     * 设置不可用状态
     * @param target 目标对象
     * @param disabled
     */
    var _setDisabled = function (target, disabled) {
        var options = $.data(target[0], _cacheName).options;
        var ico_l = target.find('span.icon_left');
        var ico_r = target.find('span.icon_right');
        //不可用时
        if (disabled || target.attr('disabled') == 'disabled') {
            options.disabled = true;
            target.attr('disabled', true);
            ico_l.addClass('iconDisabled');
            ico_r.addClass('iconDisabled');
        }
        if (!disabled) {
            options.disabled = false;
            target.attr('disabled', false);
            ico_l.removeClass('iconDisabled');
            ico_r.removeClass('iconDisabled');
        }
        _setStyle(target, options.style);
    };
    /**
     * 设置样式
     * @param target 目标对象
     * @private
     * @param style
     */
    var _setStyle = function (target, style) {
        var options = $.data(target[0], _cacheName).options;
        var disabled = options.disabled;
        //不可用时
        if (disabled) {
            target.removeClass().addClass('button button_disabled');
        }
        else {
            if (style) {
                options.style = style;
                if (style == "button") {
                    target.removeClass().addClass('button');
                }
                else {
                    target.removeClass().addClass('button ' + style);
                }
            }
        }
        if (!target.is("button")) {
            target.addClass('buttonHack');
        }
    }
})(jQuery);
